package edu.hubu.supermarketmanagementsystem.service.impl;

import edu.hubu.supermarketmanagementsystem.entity.Shelves;
import edu.hubu.supermarketmanagementsystem.mapper.ShelvesMapper;
import edu.hubu.supermarketmanagementsystem.service.ShelvesService;
import edu.hubu.supermarketmanagementsystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShelvesServiceImpl implements ShelvesService {

    // 注入 ShelvesMapper 进行数据库操作
    @Autowired
    private ShelvesMapper shelvesMapper;

    @Override
    public ResultVO<Boolean> addShelf(Shelves shelf) {
        if(shelvesMapper.addShelves(shelf)>0){
            return ResultVO.success(true);
        }
        else {
            return ResultVO.error(500, "添加货架失败。");
        }
    }

    @Override
    public ResultVO<Boolean> updateShelf(Shelves shelf) {
        if(shelvesMapper.updateShelves(shelf) > 0){
            return ResultVO.success(true);
        }
        else {
            return ResultVO.error(500, "更新货架失败。");
        }
    }

    @Override
    public ResultVO<Boolean> deleteShelf(int shelfId) {
        if(shelvesMapper.deleteShelves(shelfId)>0){
            return ResultVO.success(true);
        }
        else {
            return ResultVO.error(500, "删除货架失败。");
        }
    }

    @Override
    public ResultVO<Shelves> getShelfById(int shelfId) {
        Shelves result = shelvesMapper.findShelvesById(shelfId);
        if(result!=null){
            return ResultVO.success(result);
        }
        else {
            return ResultVO.error(500, "没有与搜索条件相匹配的项。");
        }
    }

    @Override
    public ResultVO<List<Shelves>> getAllShelves() {
        List<Shelves> result = shelvesMapper.findAllShelves();
        if(result!=null){
            return ResultVO.success(result);
        }
        else {
            return ResultVO.error(500, "不存在货架");
        }
    }

    @Override
    public List<Shelves> findShelvesByAreaId(int areaId) {
        // 调用 ShelvesMapper 的 findShelvesByAreaId 方法
        return shelvesMapper.findShelvesByAreaId(areaId);
    }

    @Override
    public List<Shelves> findShelvesByAdminId(int adminId) {
        // 调用 ShelvesMapper 的 findShelvesByAdminId 方法
        return shelvesMapper.findShelvesByAdminId(adminId);
    }
}
